package com.huluobo.mvvm2003ademo.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CompoundButton;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;

import com.huluobo.mvvm2003ademo.databinding.CartItemBinding;
import com.huluobo.mvvm2003ademo.model.CartItem;

/**
 * Created by lc on 2022/9/14.
 */
public class CartListAdapter extends ListAdapter<CartItem, CartListAdapter.CartViewHolder> {

    private CartInterface cartInterface;

    public CartListAdapter(CartInterface cartInterface) {
        super(CartItem.itemCallback);
        this.cartInterface = cartInterface;
    }

    @NonNull
    @Override
    public CartViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        CartItemBinding cartItemBinding = CartItemBinding.inflate(layoutInflater, parent, false);
        return new CartViewHolder(cartItemBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull CartListAdapter.CartViewHolder holder, int position) {
        holder.cartItemBinding.setCartItem(getItem(position));
        //强制执行页面刷新,局部刷新
        holder.cartItemBinding.executePendingBindings();
    }

    class CartViewHolder extends RecyclerView.ViewHolder {

        CartItemBinding cartItemBinding;

        public CartViewHolder(@NonNull CartItemBinding cartItemBinding) {
            super(cartItemBinding.getRoot());
            this.cartItemBinding = cartItemBinding;
            cartItemBinding.deleteProductButton.setOnClickListener(v -> {
                cartInterface.deleteCartItem(getItem(getAdapterPosition()));
            });

            cartItemBinding.quantitySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    int quantity = position + 1;
                    if (quantity != getItem(getAdapterPosition()).getQuantity()) {
                        cartInterface.changeQuantity(getItem(getAdapterPosition()), quantity);
                    }
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });

            cartItemBinding.cartCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    cartInterface.changeCheck(getItem(getAdapterPosition()), isChecked);
                }
            });
        }
    }

    public interface CartInterface {
        void deleteCartItem(CartItem cartItem);

        void changeQuantity(CartItem cartItem, int quantity);

        void changeCheck(CartItem cartItem, boolean checked);
    }
}
